package cn.edu.zjicm.wordsnet_d.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.view.View;

public class ScreenShotUtils {
	Context context;

	public ScreenShotUtils(Context context) {
		this.context = context;
	}

	public Bitmap getScreenShot() {
		Bitmap bitmap = null;
		View view = ((Activity) context).getWindow().getDecorView();
		if (false == view.isDrawingCacheEnabled()) {
			view.setDrawingCacheEnabled(true);
		}
		bitmap = view.getDrawingCache();

		Rect frame = new Rect();
		// 测量屏幕宽和高
		view.getWindowVisibleDisplayFrame(frame);
		int stautsHeight = frame.top;
		// Log.d("jiangqq", "状态栏的高度为:" + stautsHeight);

		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		// int width = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth();
		// int height = ((Activity) context).getWindowManager().getDefaultDisplay().getHeight();
		// 根据坐标点和需要的宽和高创建bitmap
		if (height > stautsHeight) {
			bitmap = Bitmap.createBitmap(bitmap, 0, stautsHeight, width, height - stautsHeight);
		}
		// BitmapDrawable bd = new BitmapDrawable(bitmap);
		// imageView.setImageDrawable(bd);
		// savePic(bitmap,"sdcard/DCIM/"+System.currentTimeMillis()+".png");
		return bitmap;
	}

	private static boolean savePic(Bitmap pBitmap, String strName) {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(strName);
			if (null != fos) {
				pBitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
				fos.flush();
				fos.close();
				return true;
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
}
